package com.nick.sscplan.ui;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nick.sscplan.bean.SscInfo;
import com.nick.sscplan.provider.SscDAO;
import com.nick.sscplan.SscData;
import com.nick.sscplan.SscOperation;
import com.nick.sscplan.util.SscUtils;

public class Ssc_PlanActivity extends ActivityGroup {
    
    private static final String TAG = "ssc_plan";
    
    
    private Button mSscGo = null;
    private EditText mSscInput = null;
    private TextView mSscShow = null;
    
    private SscDAO mSscDAO;
    private SscData mSscData;
    private List<SscInfo> sscInfos;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ssc_plan);
        initData();
        //mSscShow.setText(SscUtils.getSystemDate());
        

    }
    
    
    
    public void initData (){
//        mSscGo = (Button) findViewById(R.id.sscgo);
//        mSscInput = (EditText) findViewById(R.id.sscinput);
        mSscShow = (TextView) findViewById(R.id.sscshow);
//        mSscGo.setOnClickListener(ocl);
        mSscDAO = new SscDAO(this);
        mSscData = new SscData();
        
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads()
        .detectDiskWrites()
        .detectNetwork()   // or .detectAll() for all detectable problems
        .penaltyLog()
        .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectLeakedSqlLiteObjects()
        .penaltyLog()
        .penaltyDeath()
        .build());
        

        //SscInfo sscInfo = mSscDAO.queryByIssue(120924100);
        //mSscShow.setText(sscInfo.toString());
        List<SscInfo> list = mSscDAO.queryAllSscInfo();
        mSscShow.setText(list.toString());
        
    }
    
    public String calculate(String ssc){
        int num = Integer.valueOf(ssc);
        
        
        
        return "";
    }
    
    
    
    
    OnClickListener ocl = new OnClickListener() {
        
        public void onClick(View v) {
            String ssc = mSscInput.getText().toString();
            String subSsc = ssc.substring(2);
            String ssc_plan = calculate(subSsc);
            mSscShow.setText(ssc_plan);
        }
    };
    
    
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1, 1, 1, "刷新");
        menu.add(1, 2, 2, "预测");
        menu.add(1, 3, 3, "删除");
        return super.onCreateOptionsMenu(menu);
    }
    
    
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            refresh();
            break;
            
        case 2:
            Intent intent = new Intent(Ssc_PlanActivity.this , Ssc_HistoryActivity.class);
            startActivity(intent);
            
        case 3:
            //mSscDAO.deleteAllData();
            break;

        default:
            break;
        }
        
        
        return super.onMenuItemSelected(featureId, item);
    }
    
    public void logd(String str){
        Log.d(TAG, "" + str);
    }
    
    
    public void refresh(){
        try {
            URL url = new URL(SscUtils.getUrl());
            logd(SscUtils.getUrl());
            logd("url="+url);
            sscInfos = mSscData.initData(url); 
            if (sscInfos == null) {
                return;
            }
            for (int i = 0; i < sscInfos.size(); i++) {
                mSscDAO.insertSscInfo(sscInfos.get(i));
            }
            List<SscInfo> list = mSscDAO.queryAllSscInfo();
            mSscShow.setText(list.toString());
            

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    
    
    
    
}